// bmp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "BitMap.h"
#include <conio.h>
#include <string>
#include <vector>
#include "MyImage.h"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

string name = "D:\\sandwitchGS.bmp";
unsigned int x,y;
unsigned int pixelsX = 7, pixelsY = 7, resolution=8; //resolution=8x8
unsigned int pixelsX0 = 0, pixelsY0 = 0;
unsigned int samplesX;
unsigned int samplesY;
unsigned int samplesAll;
vector<vector<float>> samples;

int counter = 1;
try{
	MyImage<unsigned char> image(name);
	image.getImageResolution(&x,&y);
	samplesX = x/resolution;
	samplesY = y/resolution;
	samplesAll = samplesX*samplesY;
	samples.resize(samplesAll);
	for (unsigned int i=0;i<samplesAll;i++){
		MyImage<unsigned char> img(image.getImageBitMapSample(pixelsX0,pixelsY0,pixelsX,pixelsY));
		samples[i] = img.extractImageData();
		if (counter==samplesX){
			pixelsX0 =0;
			pixelsX = resolution-1;
			pixelsY0 += resolution;
			pixelsY += resolution;
			counter = 1;
		}else{
			pixelsX0 += resolution;
			pixelsX+=resolution;
			counter++;
		}
	}
}
catch(IllegalArgumentException e){ 
	printf("%s",e.what()); 
	_getch();
	exit(-1);
}
catch(FileException e){
	printf("%s",e.what()); 
	_getch();
	exit(-2);
}	

	vector<float> med;
	med.resize(samples[0].size());
	for(unsigned int j=0;j<samples[0].size();j++){
		for (unsigned int i=0; i<samples.size();i++){
			med[j] += samples[i][j];
		}
		med[j]=med[j]/samples.size();
		for (unsigned int i=0;i<samples.size();i++){
			samples[i][j]-=med[j];
		}
	}

	_getch();

	return 0;
}